﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Scenes;
using Microsoft.Xna.Framework;
using GameEngine.Managers.IManagers;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.Camera;
using PvT.Scenes.Screens;
using PvT.Entities;
using GameEngine.EntityComponents;
using PvT.Scenes.Factory;
using GameEngine.Utils;

namespace PvT.Scenes
{
    public class PauseScreen : AbstractGameScreen
    {
        private Texture2D dummyTexture;

        public PauseScreen(Game game)
            : base(game)
        {
            this.MenuTitle = "Pause Menu";
            this.dummyTexture = game.Content.Load<Texture2D>("redpicture");
            this.TransitionOnTime = TimeSpan.FromSeconds(2);
            this.TransitionOffTime = TimeSpan.FromSeconds(2);

            // Create our menu entries.
            MenuEntry playGameMenuEntry = new MenuEntry(game, "Continue Game");
            MenuEntry newGameMenuEntry = new MenuEntry(game, "Start new game");
            MenuEntry exitMenuEntry = new MenuEntry(game, "Exit");

            // Hook up menu event handlers.
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            newGameMenuEntry.Selected += NewGameMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(playGameMenuEntry);
            MenuEntries.Add(newGameMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Camera2D camera = (Camera2D)game.Services.GetService(typeof(Camera2D));

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.GetTransformation());

            float alpha = TransitionAlpha * 255;
            Color backgroundColor = Color.FromNonPremultiplied(0, 0, 0, (int)alpha/2);
            spriteBatch.Draw(dummyTexture, new Rectangle((int)camera.Pos.X - (game.GraphicsDevice.Viewport.Width/2), (int)camera.Pos.Y - (game.GraphicsDevice.Viewport.Height/2), game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height), backgroundColor);
            spriteBatch.End();

            base.Draw(spriteBatch, gameTime);
        }

        #region Handle Input

        public override void BackAction()
        {
            //ScreenState = ScreenState.TransitionOff;
            ISceneManager scene = (ISceneManager)game.Services.GetService(typeof(ISceneManager));
            scene.PopScene();
        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void NewGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            IAudioManager audioManager = (IAudioManager)game.Services.GetService(typeof(IAudioManager));
            audioManager.StopLoopedSound("sound");
            ISceneManager sceneManager = (ISceneManager)game.Services.GetService(typeof(ISceneManager));
            this.isActive = false;
            sceneManager.ClearScenes();
            Scene sceneWrapper;
            (new ConcreteFactory()).GetLevel(game, "", out sceneWrapper);
            sceneManager.PushScene(sceneWrapper);
        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenState = ScreenState.TransitionOff;
            ISceneManager scene = (ISceneManager)game.Services.GetService(typeof(ISceneManager));
            scene.PopScene();
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            game.Exit();
        }

        #endregion
    }
}
